#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "gfx/epd_gfx.h"

static int count_bits(const uint8_t *buf, size_t size)
{
    int cnt = 0;
    for (size_t i = 0; i < size; ++i) {
        uint8_t v = buf[i];
        for (int b = 0; b < 8; ++b) {
            cnt += (v >> b) & 1U;
        }
    }
    return cnt;
}

int main(void)
{
    const uint16_t W = 640, H = 384;
    const size_t plane_size = (size_t)W * (size_t)H / 8U;
    uint8_t *bw = (uint8_t *)malloc(plane_size);
    uint8_t *ry = (uint8_t *)malloc(plane_size);
    if ((bw == NULL) || (ry == NULL)) { return 1; }

    memset(bw, 0, plane_size);
    memset(ry, 0, plane_size);

    int rc = epd_gfx_build_vertical_tricolor_stripes(bw, ry, W, H);
    if (rc != 0) {
        printf("gfx build failed\n");
        return 2;
    }

    /* Basic checks: top 1/3 black => exactly W*H/3 pixels set in BW; bottom 1/3 accent => same set in RY */
    const int total_px = (int)W * (int)H;
    const int per_band_px = total_px / 3;

    const int bw_bits = count_bits(bw, plane_size);
    const int ry_bits = count_bits(ry, plane_size);

    printf("BW bits: %d, RY bits: %d, expected band: %d\n", bw_bits, ry_bits, per_band_px);

    if ((bw_bits != per_band_px) || (ry_bits != per_band_px)) {
        printf("Test failed: band bit counts mismatch\n");
        return 3;
    }

    printf("All tests passed.\n");
    free(bw); free(ry);
    return 0;
}